package academic.ui.client.widgets;

import java.util.ArrayList;
import java.util.List;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.util.ClientHelper;
import ecommerce.soco.shared.GWT_Product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

public class CompactDeals extends Composite{
	
	/**
	 * Pass in an array of GWT_Purchase and outputs the deals in a report 
	 * @param deals - recent deals
	 * 
	 */
	GenericContainerTag pageHeader = new GenericContainerTag("div");

	ArrayList<GenericClickable> clickHandlers = new ArrayList<GenericClickable>();
	
	public CompactDeals(List<GWT_Product> products) {
		GenericContainerTag report = new GenericContainerTag("div");
		GenericContainerTag h2 = new GenericContainerTag("h2","Recent Deals For Austin");
		
		GenericContainerTag dl = new GenericContainerTag("dl");
		GenericContainerTag dt = new GenericContainerTag("dt","Recent Deals");
		
		report.add(pageHeader);
		pageHeader.add(h2);
		report.add(dl);
		dl.add(dt);
		
		for(GWT_Product product: products)
		{			
			GenericContainerTag dd = new GenericContainerTag("dd");
			GenericContainerTag p = new GenericContainerTag("p",ClientHelper.formatDate(product.getDateAvailable()));
			GenericContainerTag dealDiv = new GenericContainerTag("div");
			GenericContainerTag p2 = new GenericContainerTag("p");
			GenericClickable dealLink = new GenericClickable("a",product.getName());
			dealLink.getElement().setId(String.valueOf(product.getId()));
			clickHandlers.add(dealLink);
			
			
			
			
			
			
			GenericClickable imageLink = new GenericClickable("a");
			imageLink.getElement().setId(String.valueOf(product.getId()));
			Image productImg = new Image(GWT.getModuleBaseURL()+"../PICS/Products/"+product.getImage());
			productImg.setSize("183px", "112px");
			clickHandlers.add(imageLink);
			imageLink.add(productImg);
			
			
			
			
			dealDiv.add(p2);
			p2.add(dealLink);
			dealDiv.add(new BoughtPriceDiv(product));
			dealDiv.add(imageLink);
			dd.add(p);
			dd.add(dealDiv);
			dl.add(dd);
			
			//styles
			p.setStyleName("date");
			
		}
		
		//styles
		report.setStyleName("recent");
		pageHeader.setStyleName("page_header");
		dl.setStyleName("recent_deals");
		
		initWidget(report);
	}
	
	public ArrayList<GenericClickable> getClickHandlers() {
		return clickHandlers;
	}
	
	public void displayHeader(Boolean display) {
		if(!display)
			DOM.setStyleAttribute(pageHeader.getElement(), "display", "none");
	}
}
